package app1.rapidsquare.com.core;

import app1.rapidsquare.com.Utility.Utility;
import app1.rapidsquare.com.activity.MainActivity;
import app1.rapidsquare.com.activity.R;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;

public class GsmMessageService extends IntentService {

    public static final int NOTIFICATION_ID = 101;

    public GsmMessageService() {
        super("GsmMessageService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                    .equals(messageType)) {
                Utility.Info("Send error ", extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                    .equals(messageType)) {
                Utility.Info("Deleted messages on server  ", extras.toString());

            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) {
                if (extras.containsKey("alert")) {
                    String extra = extras.getString("alert");
                    if (extra.contains(";")) {
                        String[] userData = extra.split(";");
                        if (userData.length == 3) {
                            sendNotification(getResources().getString(R.string.connection_request_title));
                            GsmBroadcastReceiver.completeWakefulIntent(intent);
                            Utility.Info("Received  ", extras.toString());
                        } else {
                            if (userData.length == 2
                                    && userData[0].equals(Utility.getUserSession(getApplicationContext()).Code)) {
                                boolean isReply = Boolean
                                        .valueOf(userData[1]);
                                if (isReply)
                                    sendNotification("Your request is confirmed");
                                else
                                    sendNotification("Your request is cancelled");
                            }
                        }

                    }
                } else {
                    Utility.Info("Error until save extra to session ", extras.toString());
                }
            }
        }

    }

    private void sendNotification(String msg) {
        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                0, new Intent(getApplicationContext(), MainActivity.class).putExtra(
                        Utility.NOTIFICATION_STATUS, ""), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.notification)
                .setTicker(msg)
                .setContentTitle("Rapid Square")
                .setContentText(msg)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        Intent intent = new Intent(Utility.NOTIFICATION_BROADCASTRECEIVER_ACTION);

        intent.putExtra("message", "notification");
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
    }
}
